<!DOCTYPE html>

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

<title>class LicenceInjector - licence_injector - Inject licence text into source files</title>

<link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
</script>

<script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>


<body id="top" class="class">
<nav id="metadata">
  <nav id="home-section" class="section">
  <h3 class="section-header">
    <a href="./index.html">Home</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </h3>
</nav>


  <nav id="search-section" class="section project-section" class="initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <h3 class="section-header">
      <input type="text" name="search" placeholder="Search" id="search-field"
             title="Type to search, Up and Down to navigate, Enter to load">
    </h3>
  </form>

  <ul id="search-results" class="initially-hidden"></ul>
</nav>


  

  <div id="file-metadata">
    <nav id="file-list-section" class="section">
  <h3 class="section-header">Defined In</h3>
  <ul>
    <li>lib/LicenceInjector.rb
  </ul>
</nav>

    
  </div>

  <div id="class-metadata">
    
    <nav id="parent-class-section" class="section">
  <h3 class="section-header">Parent</h3>
  
  <p class="link"><a href="Object.html">Object</a>
  
</nav>

    
    
    <!-- Method Quickref -->
<nav id="method-list-section" class="section">
  <h3 class="section-header">Methods</h3>

  <ul class="link-list">
    
    <li ><a href="#method-c-new">::new</a>
    
    <li ><a href="#method-i-inject_licence">#inject_licence</a>
    
    <li ><a href="#method-i-inject_licence_into_file">#inject_licence_into_file</a>
    
    <li ><a href="#method-i-read_file">#read_file</a>
    
    <li ><a href="#method-i-replace_old_licence">#replace_old_licence</a>
    
  </ul>
</nav>

  </div>

  <div id="project-metadata">
    <nav id="fileindex-section" class="section project-section">
  <h3 class="section-header">Pages</h3>

  <ul>
  
    <li class="file"><a href="./README_rdoc.html">README</a>
  
  </ul>
</nav>

    <nav id="classindex-section" class="section project-section">
  <h3 class="section-header">Class and Module Index</h3>

  <ul class="link-list">
  
    <li><a href="./LicenceInjector.html">LicenceInjector</a>
  
    <li><a href="./Licence_Injector.html">Licence_Injector</a>
  
    <li><a href="./Object.html">Object</a>
  
    <li><a href="./OptionParser.html">OptionParser</a>
  
  </ul>
</nav>

  </div>
</nav>

<div id="documentation">
  <h1 class="class">class LicenceInjector</h1>

  <div id="description" class="description">
    
  </div><!-- description -->

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    
    <!-- Attributes -->
    <section id="attribute-method-details" class="method-section section">
      <h3 class="section-header">Attributes</h3>

      
      <div id="attribute-i-changed_files_count" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">changed_files_count</span><span
            class="attribute-access-type">[R]</span>
        </div>

        <div class="method-description">
        
        
        
        </div>
      </div>
      
      <div id="attribute-i-extensions" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">extensions</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        
        
        </div>
      </div>
      
      <div id="attribute-i-licence_file_path" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">licence_file_path</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        
        
        </div>
      </div>
      
      <div id="attribute-i-old_licence_file_path" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">old_licence_file_path</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        
        
        </div>
      </div>
      
      <div id="attribute-i-src_path" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">src_path</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        
        
        </div>
      </div>
      
    </section><!-- attribute-method-details -->
    

    <!-- Methods -->
    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Class Methods</h3>

    
      <div id="method-c-new" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">new</span><span
            class="method-args">(command, licence_file_path, src_path, file_extensions, old_licence_file_path = "", list = false, overwrite = true)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File lib/LicenceInjector.rb, line 6</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">licence_file_path</span>, <span class="ruby-identifier">src_path</span>, <span class="ruby-identifier">file_extensions</span>, <span class="ruby-identifier">old_licence_file_path</span> = <span class="ruby-string">&quot;&quot;</span>, <span class="ruby-identifier">list</span> = <span class="ruby-keyword">false</span>, <span class="ruby-identifier">overwrite</span> = <span class="ruby-keyword">true</span>)
        <span class="ruby-ivar">@licence_file_path</span> = <span class="ruby-identifier">licence_file_path</span>
        <span class="ruby-ivar">@old_licence_file_path</span> = <span class="ruby-identifier">old_licence_file_path</span>
        <span class="ruby-ivar">@src_path</span> = <span class="ruby-identifier">src_path</span>
        <span class="ruby-ivar">@extensions</span> = <span class="ruby-identifier">file_extensions</span>
        <span class="ruby-ivar">@changed_files_count</span> = <span class="ruby-value">0</span>
        <span class="ruby-ivar">@command</span> = <span class="ruby-identifier">command</span>
        <span class="ruby-ivar">@list</span> = <span class="ruby-identifier">list</span>
        <span class="ruby-ivar">@overwrite</span> = <span class="ruby-identifier">overwrite</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- new-source -->
          
        </div>

        

        
      </div><!-- new-method -->

    
    </section><!-- public-class-method-details -->
  
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Instance Methods</h3>

    
      <div id="method-i-inject_licence" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">inject_licence</span><span
            class="method-args">()</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="inject_licence-source">
            <pre><span class="ruby-comment"># File lib/LicenceInjector.rb, line 23</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">inject_licence</span>
        <span class="ruby-ivar">@changed_files_count</span> = <span class="ruby-value">0</span>
        <span class="ruby-ivar">@extensions</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">extension</span> <span class="ruby-operator">|</span>
                <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@list</span>
                        <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;\tprocessing #{extension} files in #{@src_path}&quot;</span>
                        <span class="ruby-identifier">print</span> <span class="ruby-string">&quot;\t&quot;</span>
                <span class="ruby-keyword">end</span>
                <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">glob</span>(<span class="ruby-ivar">@src_path</span> <span class="ruby-operator">+</span> <span class="ruby-node">&quot;/**/*.#{extension}&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span> <span class="ruby-comment">#find src files in current folder and all subfolders</span>
                        <span class="ruby-keyword">case</span>  
                                <span class="ruby-keyword">when</span> <span class="ruby-ivar">@command</span> <span class="ruby-operator">==</span> <span class="ruby-value">:inject</span>
                                        <span class="ruby-identifier">inject_licence_into_file</span> <span class="ruby-identifier">file</span>
                                <span class="ruby-keyword">when</span> <span class="ruby-ivar">@command</span> <span class="ruby-operator">==</span> <span class="ruby-value">:replace</span>
                                        <span class="ruby-identifier">replace_old_licence</span> <span class="ruby-identifier">file</span>
                        <span class="ruby-keyword">end</span>
                <span class="ruby-keyword">end</span>
                <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@list</span>
                        <span class="ruby-identifier">puts</span>
                <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@list</span>
                <span class="ruby-identifier">puts</span>
        <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@list</span>
                <span class="ruby-keyword">if</span> <span class="ruby-ivar">@overwrite</span>
                        <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;There were #{@changed_files_count} changes made&quot;</span>
                <span class="ruby-keyword">else</span>
                        <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{@changed_files_count} file(s) were found. Use the --overwrite flag to make changes to the files&quot;</span>
                <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- inject_licence-source -->
          
        </div>

        

        
      </div><!-- inject_licence-method -->

    
      <div id="method-i-inject_licence_into_file" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">inject_licence_into_file</span><span
            class="method-args">(file)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="inject_licence_into_file-source">
            <pre><span class="ruby-comment"># File lib/LicenceInjector.rb, line 79</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">inject_licence_into_file</span> <span class="ruby-identifier">file</span>
        <span class="ruby-comment"># possible refactor here - using an array to read the src so we can easily check the </span>
        <span class="ruby-comment"># first line for special content. If we used a string we could reuse the read file method </span>
        <span class="ruby-comment"># but lose the nice array methods... Instinct is to use array</span>
        <span class="ruby-identifier">old_content</span> = []
        <span class="ruby-identifier">newcontent</span> = []
        <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file</span>, <span class="ruby-string">&quot;r&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">f</span> <span class="ruby-operator">|</span>
                <span class="ruby-identifier">old_content</span> = <span class="ruby-identifier">f</span>.<span class="ruby-identifier">readlines</span>
        <span class="ruby-keyword">end</span>

        <span class="ruby-comment">#some files need to have their first lines maintained such as xml files and files using a shebang</span>
        <span class="ruby-keyword">unless</span> <span class="ruby-identifier">old_content</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># empty file</span>
                <span class="ruby-keyword">if</span> <span class="ruby-identifier">old_content</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-node">&quot;#!&quot;</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">old_content</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&quot;&lt;?xml&quot;</span>)
                        <span class="ruby-identifier">newcontent</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">old_content</span>.<span class="ruby-identifier">first</span>
                        <span class="ruby-identifier">old_content</span>.<span class="ruby-identifier">shift</span>
                <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">end</span>
        
        <span class="ruby-identifier">newcontent</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">read_file</span> <span class="ruby-ivar">@licence_file_path</span>
        <span class="ruby-identifier">newcontent</span> = <span class="ruby-identifier">newcontent</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">old_content</span>
        
        <span class="ruby-keyword">if</span> <span class="ruby-ivar">@overwrite</span>
                <span class="ruby-keyword">begin</span>
                        <span class="ruby-identifier">output</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">file</span>, <span class="ruby-string">&quot;w&quot;</span>)
                        <span class="ruby-identifier">newcontent</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> <span class="ruby-identifier">output</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">line</span> }
                        <span class="ruby-identifier">output</span>.<span class="ruby-identifier">close</span>
                <span class="ruby-keyword">rescue</span>
                        <span class="ruby-identifier">puts</span>
                        <span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;ERROR: There was a problem writing to &#39;#{file}&quot;</span>
                        <span class="ruby-identifier">exit</span> <span class="ruby-value">1</span>
                <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">end</span>
        
        <span class="ruby-keyword">if</span> <span class="ruby-ivar">@list</span>
                <span class="ruby-identifier">puts</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">absolute_path</span>(<span class="ruby-identifier">file</span>)
        <span class="ruby-keyword">else</span>
                <span class="ruby-identifier">print</span> <span class="ruby-string">&quot;.&quot;</span>
        <span class="ruby-keyword">end</span>
        <span class="ruby-ivar">@changed_files_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- inject_licence_into_file-source -->
          
        </div>

        

        
      </div><!-- inject_licence_into_file-method -->

    
      <div id="method-i-read_file" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">read_file</span><span
            class="method-args">(file_path)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="read_file-source">
            <pre><span class="ruby-comment"># File lib/LicenceInjector.rb, line 17</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">read_file</span> <span class="ruby-identifier">file_path</span>
        <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span> <span class="ruby-identifier">file_path</span>, <span class="ruby-string">&quot;r&quot;</span>  <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">file</span> <span class="ruby-operator">|</span>
                <span class="ruby-identifier">file</span>.<span class="ruby-identifier">read</span>
        <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- read_file-source -->
          
        </div>

        

        
      </div><!-- read_file-method -->

    
      <div id="method-i-replace_old_licence" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">replace_old_licence</span><span
            class="method-args">(file)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="replace_old_licence-source">
            <pre><span class="ruby-comment"># File lib/LicenceInjector.rb, line 54</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">replace_old_licence</span> <span class="ruby-identifier">file</span>
        <span class="ruby-identifier">src</span> = <span class="ruby-identifier">read_file</span> <span class="ruby-identifier">file</span>
        
        <span class="ruby-identifier">src</span> = <span class="ruby-identifier">src</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-identifier">read_file</span>(<span class="ruby-ivar">@old_licence_file_path</span>), <span class="ruby-identifier">read_file</span>(<span class="ruby-ivar">@licence_file_path</span>))
                
        <span class="ruby-keyword">if</span> <span class="ruby-ivar">@overwrite</span>
                <span class="ruby-keyword">begin</span>
                        <span class="ruby-identifier">output</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">file</span>, <span class="ruby-string">&quot;w&quot;</span>)
                        <span class="ruby-identifier">output</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">src</span>
                        <span class="ruby-identifier">output</span>.<span class="ruby-identifier">close</span>
                <span class="ruby-keyword">rescue</span>
                        <span class="ruby-identifier">puts</span>
                        <span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;ERROR: There was a problem writing to &#39;#{file}&quot;</span>
                        <span class="ruby-identifier">exit</span> <span class="ruby-value">1</span>
                <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">end</span>
        
        <span class="ruby-keyword">if</span> <span class="ruby-ivar">@list</span>
                <span class="ruby-identifier">puts</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">absolute_path</span>(<span class="ruby-identifier">file</span>)
        <span class="ruby-keyword">else</span>
                <span class="ruby-identifier">print</span> <span class="ruby-string">&quot;.&quot;</span>
        <span class="ruby-keyword">end</span>
        <span class="ruby-ivar">@changed_files_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- replace_old_licence-source -->
          
        </div>

        

        
      </div><!-- replace_old_licence-method -->

    
    </section><!-- public-instance-method-details -->
  
  </section><!-- 5Buntitled-5D -->

</div><!-- documentation -->


<footer id="validator-badges">
  <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
  <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 4.0.1.
  <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
</footer>

